package com.lbh.cloudgallery.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lbh.cloudgallery.entity.AddressEntity;
import com.lbh.cloudgallery.service.AddressService;
import com.xxx.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 凌月青缃
 * @date 2021/3/2
 */
@Api(tags = "前台-地址模块")
@RestController
@RequestMapping("front/address")
public class FrontAddressController {

    @Autowired
    AddressService addressService;
    /**
     * 详情页加载收藏状态
     */
    @ApiOperation(value = "根据用户id查询所有地址")
    @GetMapping("/getById/{memberId}")
    public R getById(@PathVariable("memberId") Long memberId){
        List<AddressEntity> list = addressService.list(new QueryWrapper<AddressEntity>().eq("receiver_id", memberId));
        return R.ok().put("list",list);
    }

    @ApiOperation(value = "删除指定地址,我都拼接在路径上了，为了你好传值")
    @DeleteMapping("/removeAddress")
    public R removeAddress(@RequestParam("memberId") Long memberId,@RequestParam("addressId") Long addressId){
        boolean remove = addressService.remove(new QueryWrapper<AddressEntity>().eq("receiver_id", memberId).eq("id", addressId));
        if (remove){
            return R.ok().put("msg","删除成功！");
        }else {
            return R.error().put("msg","删除失败！");

        }
    }


    /**
     * 保存
     */
    @ApiOperation(value = "新增地址,复用后台接口,receiver_id为memberId,有些值不需要传，比如id，更新修改日期这个没有，不需要传")
    @PostMapping("/addAddress")
    public R addAddress(@RequestBody AddressEntity address){
        addressService.save(address);
        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改地址,复用后台接口，id为地址Id，receiver_id为memberId")
    @PostMapping("/updateAddress")
    public R updateAddress(@RequestBody AddressEntity address){
        addressService.updateById(address);

        return R.ok();
    }



}
